package com.css.dzj.dzjfxyj.modules.distribute.Controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjfxyj.common.audit.service.SysAuditService;
import com.css.dzj.dzjfxyj.common.util.*;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.acquier.service.AcquierService;
import com.css.dzj.dzjfxyj.modules.distribute.entity.JgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.distribute.service.DistributeService;
import com.css.dzj.dzjfxyj.modules.distribute.vo.DistributeEditVo;
import com.css.dzj.dzjfxyj.modules.distribute.vo.DistributeVo;
import com.querydsl.core.QueryResults;
import dm.jdbc.stat.support.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 风险预警分发
 */
@RestController
@RequestMapping("/fenfa")
@PropertySource(value = "classpath:base.properties")
public class DistributeController {
    @Autowired
    DistributeService distributeService;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 分页查询所有(分发)
     */
    @GetMapping(value = "/getAllDistribute")
    public R getAllDistribute(@RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize,
                              @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                              @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                              @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                              @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                              @RequestParam(name = "startTime1", required = false) String startTime1,
                              @RequestParam(name = "endTime1", required = false) String endTime1,
                              @RequestParam(name = "startTime", required = false) String startTime,
                              @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults results = distributeService.selectAll(fxyjDomainId, fxyjTitle, fxyjLevel, fxyjObjectName, startTime1, endTime1, startTime, endTime, pageRequest);
        //baseService.logSuccess(3, "查询分发成功", "2");
        String s = JSON.toJSONString(results);
        sysAuditService.saveSuccessAudit("分页查询所有(分发)成功", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", results);
    }


    /***
     * 保存分发
     * @param riskwarningEdit
     * @return
     */
    @PostMapping("/fenFaSave")
    public R fenFaSave(@RequestBody DistributeEditVo riskwarningEdit, HttpServletRequest request) {
        String s = "";
        try {
            distributeService.saveFenFa(riskwarningEdit, request);
            //baseService.logSuccess(3, "分发保存成功", "2");
            s = JSON.toJSONString(riskwarningEdit);
            sysAuditService.saveSuccessAudit("保存分发成功", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            //baseService.logFailed(3, "分发保存失败", "2");
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("保存分发失败", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @PostMapping("/findfenfa")
    public R findById(String id) {
        DistributeVo distributeVo = distributeService.findById(id);
        //baseService.logSuccess(3, "查询成功", "2");
        String s = JSON.toJSONString(distributeVo);
        sysAuditService.saveSuccessAudit("根据id查询成功", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", distributeVo);
    }

    @PostMapping("/updFenfa")
    public R updFenfa(JgFxyjFenfaEntity jgFxyjFenfaEntity) {
        try {
            distributeService.updFenfa(jgFxyjFenfaEntity);
            String s = JSON.toJSONString(jgFxyjFenfaEntity);
            sysAuditService.saveSuccessAudit("更新分发成功", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
            //baseService.logSuccess(3, "更新成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "更新失败", "2");
            String s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("更新分发失败", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 点击分发按钮回显数据
     *
     * @param id
     * @return
     */
    @PostMapping("/getAcquireById")
    public R getAcquireById(String id, HttpServletRequest request) {
        DistributeEditVo d = distributeService.getAcquireById(id, request);
        //baseService.logSuccess(3, "分发回显数据", "2");
        String s = JSON.toJSONString(d);
        sysAuditService.saveSuccessAudit("点击分发按钮回显数据成功", Constant.AUDIT_MODULE_FENFA, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
        return R.ok().put("data", d);
    }

}
